package com.xydtech.common.wechat.util;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.exception.ApiException;
import com.xydtech.common.wechat.config.WechatMiniProgramConfig;
import com.xydtech.common.wechat.domin.GetWechatQRCodeResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.text.MessageFormat;
import java.util.*;

/**
 * 获取小程序二维码的工具类
 *
 * @author yjs
 * @date 2022年12月09日 9:28
 */
@Slf4j
public class WeChatQrCodeUtil {
    /**
     * 获取小程序二维码公共方法
     *
     * @param
     * @return com.xydtech.common.core.domain.AjaxResult
     * @throws
     * @author Daniel
     * @date 2022/2/9 14:18
     */
    public static GetWechatQRCodeResponse getMiniProgramQRCode(String accessToken, String params) {
        //根据access_token获取小程序码
        RestTemplate restTemplate = new RestTemplate();
        WechatMiniProgramConfig.WechatMiniProgramRepoConverter wechatMiniProgramRepoConverter = new WechatMiniProgramConfig.WechatMiniProgramRepoConverter();
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
        messageConverters.add(wechatMiniProgramRepoConverter);
        messageConverters.addAll(restTemplate.getMessageConverters());
        restTemplate.setMessageConverters(messageConverters);

        String getQRCodeUrl = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}";
        getQRCodeUrl = MessageFormat.format(getQRCodeUrl, accessToken);

        JSONObject getQRCodeParamsObj = new JSONObject();
        getQRCodeParamsObj.put("scene", params);

        ResponseEntity<GetWechatQRCodeResponse> responseEntity = restTemplate.postForEntity(getQRCodeUrl, JSONObject.toJSONString(getQRCodeParamsObj), GetWechatQRCodeResponse.class);
        return  responseEntity.getBody();
    }
    /**
     * 获取小程序二维码公共方法--请求bigdata
     *
     * @param
     * @return com.xydtech.common.core.domain.AjaxResult
     * @throws
     * @author Daniel
     * @date 2022/2/9 14:18
     */
    public static GetWechatQRCodeResponse getMiniProgramQRCodeToBigData(String apiServiceUrl,String accessToken, String params) {
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("accessToken",accessToken);
        map.add("params",params);
        String apiUrl = apiServiceUrl + "/api/token/getMiniProgramQRCode";
        log.info("获取小程序二维码接口地址---------"+apiUrl);
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(map, headers);
        GetWechatQRCodeResponse wechatQRCodeResponse = new RestTemplate().postForObject(apiUrl, httpEntity, GetWechatQRCodeResponse.class);
        log.info("获取小程序二维码接口返回结果resEntity---------"+wechatQRCodeResponse);
        return  wechatQRCodeResponse;
    }
}
